package com.guodexian.xiaomi.controller;

import com.alibaba.fastjson.JSON;
import com.guodexian.xiaomi.util.HttpResponseUtil;
import com.guodexian.xiaomi.util.RedisUtil;
import com.guodexian.xiaomi.vo.ResultVO;
import com.wf.captcha.ArithmeticCaptcha;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 类说明：  <br>
 *
 * @author 南阳德刚<br>
 * 2024-12-30 14:16<br>
 * @since 1.0 <br>
 * 培养IT高薪技术人才的摇篮---北京学佳澳。<br>
 * 点击<a href="http://www.xuejiaao.com/images/qrcode.jpg">学佳澳教育</a>关注公共号,有惊喜(⊙o⊙)<br>
 */
@WebServlet("/code.png")
public class CaptchaController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
        String verCode = captcha.text().toLowerCase();
        String key = UUID.randomUUID().toString();
        // 存入redis并设置过期时间为30分钟
        RedisUtil.getJedis().setex(key, 30000, verCode);
        // 将key和base64返回给前端
        Map<String, Object> map = new HashMap<>();
        map.put("image", captcha.toBase64());
        map.put("key", key);

        HttpResponseUtil.response(resp, ResultVO.success(map));
    }
}